#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
import os
from sets import Set

TOP = 30
hot_topic = Set([])
def count_hour(obj):
    global hot_topic
    result = []
    status = obj['status']
    time = obj['time'][8:10]
    num = 1 + obj['retweet']
    for topic in hot_topic:
        if 1 == status:
            if topic in obj['quoted_quoted_text'].lower().split():
                num += obj['quoted_retweet']
                result.append('%s-%s\t%d' % (topic, time, num))
        elif 2 == status:
            if topic in obj['retweeted_text'].lower().split():
#                 num -= obj['retweeted_retweet']
                result.append('%s-%s\t%d' % (topic, time, num))
        elif 3 == status:
            if topic in obj['quoted_quoted_text'].lower().split():
                num += obj['quoted_retweet']
                result.append('%s-%s\t%d' % (topic, time, num))
            if topic in obj['retweeted_text'].lower().split():
                num += obj['retweeted_retweet']
                result.append('%s-%s\t%d' % (topic, time, num))
        else:
            if topic in obj['text'].lower().split():
#                 num += obj['retweet']
                result.append('%s-%s\t%d' % (topic, time, num))
    return result

def get_hot_topic(date):
    global hot_topic
    global TOP
    counter = 0
    with open('/home/ivy/git/Social/Social/resource/hot_topic/%s.txt' % date) as f:
        for line in f:
            if counter >= TOP:
                break
            hot_topic.add(line.split('\t')[0])
            counter += 1
    f.close()

def main():
    input_file = os.environ["map_input_file"].split('.')[0].split('/')
    date = input_file[len(input_file)-1]
    
    #get hot topic this day
    get_hot_topic(date)
    
    #topic-hour \t 1
    for line in sys.stdin:
        obj = json.loads(line)
        result = count_hour(obj)
        for item in result:
            print item

if __name__ == '__main__':
    main()     
    
